<?php
namespace App\Http\ViewComposers;

use App\Events\SystemNotify;
use App\Models\ShenpiDetail;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use App\Models\Group;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Request;
use App\Models\User;
use App\Models\RoleUser;
use App\Models\RewardPunishDetail;



class UserMenuComposer
{
    private $user;
    private $corpid;
    private $role_array;
    private $count;

    public function __construct()
    {
        if(empty(Auth::user())){
            abort(403,'登录信息有误');
        }
        $user = Auth::user()->comuser();
        if(empty($user)){
            abort(403,'登录信息有误');
        }
        $this->user = $user;
        $this->corpid = $user->corpid;
        
        $role = RoleUser::where('corpid',$this->user->corpid)->where('user_id',$this->user->id)->lists('role_id');
        $this->role_array = $role->toArray();
        
        $this->count = 0;
        if($this->user->is_leader == 1){
            $rpcount = RewardPunishDetail::where('corpid',$this->user->corpid)->where('is_effective','0')->where('current_group_id',$this->user->group_id)->count();
            $shenpicount  = ShenpiDetail::where('corpid',$this->corpid)->where('status','0')->where('current_group_id',$this->user->group_id)->count();
            $this->count = $shenpicount + $rpcount;
        }

    }
    
    public function compose(View $view)
    {
        $view->with('role',$this->role_array);
        $view->with('rp_count',$this->count);
    }
}